package com.thinkinghead.mahjongrecorder;

import java.util.Calendar;
import java.util.Date;

import android.app.ActionBar;
import android.app.DatePickerDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.thinkinghead.mahjongrecorder.ui.dialog.DialogHelper;
import com.thinkinghead.mahjongrecorder.ui.textdialog.OnTextListener;
import com.thinkinghead.mahjongrecorder.ui.textdialog.TextDialog;
import com.thinkinghead.mahjongrecorder.utils.TypeFaceManager;

public class NewRecordActivity extends BasicActivity {
	private static String TAG = "NewRecordActivity";
	private TextView txt_requestDt, txt_player1, txt_player2, txt_player3,
			txt_player4 = null;
	private EditText txt_remark;
	private Button btn_next = null;

	// instance variables
	private Date recordDate = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_game_record);
		initView();
		initBtnListner();
		initDateListener();
		initFontFace();
		fnSetRecordDt(new Date());
		fnTitle();
	}

	private void fnTitle() {
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("Create New Record");
	}

	private void initView() {
		txt_requestDt = (TextView) this.findViewById(R.id.txt_request_dt);
		txt_remark = (EditText) this.findViewById(R.id.txt_remark);
		btn_next = (Button) this.findViewById(R.id.btn_next);
	}

	private void fnSetRecordDt(Date d) {
		if (d != null) {
			recordDate = d;
		}
		String reqDtStr = (String) android.text.format.DateFormat.format(
				"yyyy-MM-dd", recordDate);
		txt_requestDt.setText(reqDtStr);
	}

	private void initFontFace() {
		Typeface tf = TypeFaceManager.getTypeFace(getAssets());
		if (tf != null) {
			txt_requestDt.setTypeface(tf);
			btn_next.setTypeface(tf);
			txt_player1.setTypeface(tf);
			txt_player2.setTypeface(tf);
			txt_player3.setTypeface(tf);
			txt_player4.setTypeface(tf);
		}
	}

	private void initBtnListner() {
		View v1 = this.findViewById(R.id.player_frame_1);
		txt_player1 = (TextView) v1.findViewById(R.id.player_name);
		txt_player1.setText(this.getString(R.string.img_player_1));
		fnAddInputNameListener(txt_player1);

		View v2 = this.findViewById(R.id.player_frame_2);
		txt_player2 = (TextView) v2.findViewById(R.id.player_name);
		txt_player2.setText(this.getString(R.string.img_player_2));
		fnAddInputNameListener(txt_player2);

		View v3 = this.findViewById(R.id.player_frame_3);
		txt_player3 = (TextView) v3.findViewById(R.id.player_name);
		txt_player3.setText(this.getString(R.string.img_player_3));
		fnAddInputNameListener(txt_player3);

		View v4 = this.findViewById(R.id.player_frame_4);
		txt_player4 = (TextView) v4.findViewById(R.id.player_name);
		txt_player4.setText(this.getString(R.string.img_player_4));
		fnAddInputNameListener(txt_player4);
	}

	private void initDateListener() {
		final Context ctx = this;

		txt_requestDt.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent arg1) {
				final DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						recordDate.setYear((year - 1900));
						recordDate.setMonth(monthOfYear);
						recordDate.setDate(dayOfMonth);
						fnSetRecordDt(null);

						String setDt = String.valueOf(year) + "-"
								+ String.valueOf(monthOfYear + 1) + "-"
								+ String.valueOf(dayOfMonth);
						Log.d(TAG, setDt);
					}
				};
				Calendar c = Calendar.getInstance();
				c.setTime(recordDate);
				int mYear = c.get(Calendar.YEAR);
				int mMonth = c.get(Calendar.MONTH);
				int mDay = c.get(Calendar.DATE);
				DatePickerDialog dpd = new DatePickerDialog(ctx,
						myDateSetListener, mYear, mMonth, mDay);
				dpd.setTitle(R.string.dialog_title_record_date);
				dpd.show();
				return false;
			}

		});
	}

	private void fnAddInputNameListener(TextView tv) {
		final Context ctx = this;

		tv.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent arg1) {
				final TextView tv = (TextView) v;
				final OnTextListener mlistener = new OnTextListener() {
					@Override
					public void textValue(String t) {
						tv.setText(t);
					}
				};
				TextDialog td = new TextDialog(ctx, mlistener, tv.getText()
						.toString());
				td.setTitle("Please input a player name.");
				td.show();
				return false;
			}

		});
	}

	public void fnNext(View v) {
		fnPreSave();
		fnSave();
	}

	private void fnSave() {
		final Context ctx = this;
		final ParseUser user = ParseUser.getCurrentUser();
		final ParseObject gRecord = new ParseObject("Record");
		gRecord.put("createdby", user);
		gRecord.put("recorddt", recordDate);

		gRecord.put("name_p1", txt_player1.getText().toString());
		gRecord.put("name_p2", txt_player2.getText().toString());
		gRecord.put("name_p3", txt_player3.getText().toString());
		gRecord.put("name_p4", txt_player4.getText().toString());
		gRecord.put("remark", txt_remark.getText().toString());

		ParseRelation<ParseObject> relation = gRecord.getRelation("subscribe");
		relation.add(user);

		gRecord.saveInBackground(new SaveCallback() {
			@Override
			public void done(ParseException e) {
				Log.d(TAG, "OID : " + gRecord.getObjectId() + "error : " + e);
				if (e == null) {
					fnPostSave();

					ParseRelation<ParseObject> relation = user
							.getRelation("likes");
					relation.add(gRecord);
					user.saveInBackground();

				} else {
					DialogHelper.getOkAlertDialog(ctx,
							"Error while saving, please try again",
							e.getMessage()).show();
				}
			}
		});
	}

	private void fnPreSave() {
		DialogHelper.fnShowDialog(this);
	}

	private void fnPostSave() {
		Log.d(TAG, "Saved!");
		DialogHelper.fnCloseDialog();
		finish();
	}
}
